﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using EShop.BussinessObjects;
using System;

namespace EShop.Common
{
     
    /// <summary> 
    /// Summary description for CGlobalData
    /// </summary>
    public class CGlobalData
    {
        public CGlobalData()
        {
        }

        public static int GetCurrentCompanyID()
        {
            Company _company = GetCurrentCompany();
            if (_company != null)
                return _company.ID;
            return Convert.ToInt32(cnstDatabase.DBOperationResult.NullValue);
        }

        public static Company GetCurrentCompany()
        {
            Company _company_Filter = new Company();
            CompanyAddress _companyAddress_Filter = new CompanyAddress();
            _companyAddress_Filter.UrldOmainAddress = HttpContext.Current.Request.Url.Host;
            _company_Filter.CompanyAddress.Add(_companyAddress_Filter);
            _company_Filter.ID = (int)cnstDatabase.DBOperationResult.ValueIsNotEffective;
            List<Company> _companyList = DataAccess.CDACompany.Select(_company_Filter);
            if (_companyList.Count > 0)
                return _companyList.First();
            return null;
        }

        public static void SetGlobalData(User user)
        {
            HttpContext.Current.Session["CurrentUser"]=user;
            HttpContext.Current.Session["glob"] = user.ID + "," + 
                user.Contact.FirstName + "," +
                user.SecurityLevel + "," + 
                user.Contact.ID+","+
                user.LoginID;
        }

        public static User GetCurrentUser()
        {
            return (User)HttpContext.Current.Session["CurrentUser"];
        }

        public static string GetCurrentLoginID()
        {
            return HttpContext.Current.Session["glob"].ToString().Split(',')[4];
        }

        public static int GetCurrentUserID()
        {
            return Convert.ToInt32(HttpContext.Current.Session["glob"].ToString().Split(',')[0]);
        }

        public static string GetCurrentFirstName()
        {
            return HttpContext.Current.Session["glob"].ToString().Split(',')[1];
        }

        public static int GetCurrentContactID()
        {
            return Convert.ToInt32(HttpContext.Current.Session["glob"].ToString().Split(',')[3]);
        }

        public static bool IsLoggedIn()
        {
            return !(HttpContext.Current.Session["glob"] == null);
        }

        public static void LogOut()
        {

            HttpContext.Current.Session["glob"] = null;
        }

    }
}
